package com.scott.crm.role.controller;

import com.scott.base.BaseBean;
import com.scott.base.message.DataRes;
import com.scott.base.message.DataUtil;
import com.scott.base.utils.AESUtil;
import com.scott.crm.role.bean.RoleBean;
import com.scott.crm.role.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 黄后林
 * @version 1.0
 * @note
 * @date 2021-11-27 16:28:55
 */
@CrossOrigin
@RestController
@Slf4j
public class RoleController {

    @Autowired
    private RoleService service;

    @RequestMapping(value = "/role/add",method = RequestMethod.POST)
    public DataRes add(@RequestBody RoleBean role) throws Exception{

        if(role == null){
            return null;
        }

        log.info(role.toString());
        int counter = service.addRole(role);
        if(counter ==1 ){
            return DataUtil.success(role);
        }else{
            return DataUtil.errors();
        }
    }
    @RequestMapping(value = "/role/update",method = RequestMethod.POST)
    public DataRes update(@RequestBody RoleBean role) throws Exception{

        if(role == null){
            return null;
        }

        log.info(role.toString());
        int counter = service.updateRole(role);
        if(counter ==1 ){
            return DataUtil.success(role);
        }else{
            return DataUtil.errors();
        }
    }
    @RequestMapping(value = "/role/delete",method = RequestMethod.POST)
    public DataRes delete(@RequestBody Map<String,List<Integer>> map) throws Exception{

        if(CollectionUtils.isEmpty(map)){
            return DataUtil.errors();
        }

        List<Integer> ids = map.get("ids");

        log.info(ids.toString());
        int counter = service.deleteRoles(ids);
        if(counter >0){
            return DataUtil.success(counter);
        }else{
            return DataUtil.errors();
        }
    }

    /**
     * 方法描述:
     * @author: 黄后林
     * @date: 2021/11/28 12:32
     * @param pageNumber 第几页
     * @param pageSize   1页多少条
     * @return com.scott.base.message.DataRes
     */
    @RequestMapping(value = "/role/list",method = RequestMethod.GET)
    public DataRes list(@RequestParam(value="pageNumber",defaultValue = "1") Integer pageNumber,
                        @RequestParam(value="pageSize",defaultValue = "5") Integer pageSize) throws Exception{
        log.info(String.valueOf(pageNumber));
        log.info(String.valueOf(pageSize));
        if(pageNumber<1){
            pageNumber=1;
        }
        Integer begin=(pageNumber-1)*pageSize;
        Integer end =pageSize;

        List<RoleBean> roleList = service.findRoleList(begin,end);

        Map<String,Object> rs= new HashMap<>();
        rs.put("roleList",roleList);

        //pageNumber 第几页
        rs.put("pageNumber",pageNumber);
        //pageSize   1页多少条
        rs.put("pageSize",pageSize);

        List<RoleBean> allRoles = service.findAllRoles();
        if(!CollectionUtils.isEmpty(allRoles)){
            Integer totalCounter = allRoles.size();
            //totalCounter 一共多少条记录
            rs.put("totalCounter",totalCounter);

            //totalPage 共多少页
            Integer totalPage = totalCounter%pageSize==0?totalCounter/pageSize:totalCounter/pageSize+1;
            rs.put("totalPage",totalPage);

        }
        log.info("roleList="+roleList.toString());
        return DataUtil.success(rs);
    }

    @RequestMapping(value = "/user/roles",method = RequestMethod.POST)
    public DataRes findRoleListByUserId(@RequestBody BaseBean base) throws Exception {


        String token=base.getToken();
        System.out.println("token="+token);
        // 解密拿到userId
        String userId = AESUtil.decrypt(token, AESUtil.KEY);
        System.out.println(userId);
        List<RoleBean> roleList = service.findRoleListByUserId(Integer.parseInt(userId));

        if(!CollectionUtils.isEmpty(roleList)){

            //本次版本只处理1个用户只有1个角色的情况
            RoleBean role = roleList.get(0);
            role.setToken(token);

           if(null != role){
               return DataUtil.success(role);
           }

        }


        return null;
    }
}
